package com.hedl.practice.diyJava.reflex.dynamic_proxy.jdk_dynamic_proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Date;

public class LogHandler implements InvocationHandler {

    Object target;      //被代理的对象，实际的方法执行者

    public LogHandler(Object target){
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        before();
        Object result = method.invoke(target, args);//调用target的method方法
        after();
        return result;
    }

    // 调用invoke方法之后执行
    private void after() {
        System.out.println(String.format("log start time [%s]",new Date()));
    }

    //调用invoke方法之前执行
    private void before() {
        System.out.println(String.format("log and time [%s]",new Date()));
    }
}
